package com.zrrd.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zrrd.reggie.bean.*;
import com.zrrd.reggie.mapper.OrdersMapper;
import com.zrrd.reggie.service.*;
import com.zrrd.reggie.util.BaseContext;
import com.zrrd.reggie.util.CustException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

@Service
public class OrdersSeviceImpl extends ServiceImpl<OrdersMapper, Orders> implements OrdersService {

    @Autowired
    private ShoppingCartService shoppingCartService;
    @Autowired
    private UserService userService;
    @Autowired
    private AddressBookService addressBookService;
    @Autowired
    private OrderDetailService orderDetailService;
    @Override
    @Transactional
    public void saveOrder(Orders orders) throws CustException {
        Long userId = BaseContext.getCurrentId();
        QueryWrapper<ShoppingCart> wrapper=new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        List<ShoppingCart> shoppingCartList = shoppingCartService.list(wrapper);
        if(shoppingCartList==null||shoppingCartList.size()==0){
            throw new CustException("购物车为空,不能下单!");
        }
        User user = userService.getById(userId);
        AddressBook addressBook = addressBookService.getById(orders.getAddressBookId());
        if(addressBook==null){
            throw new CustException("用户地址信息有误,不能下单！");
        }
        long orderNumber = IdWorker.getId();
        List<OrderDetail> detailList=new ArrayList<>();
        AtomicInteger amount = new AtomicInteger(0);
        for (ShoppingCart shoppingCart : shoppingCartList) {
            OrderDetail detail=new OrderDetail();
            detail.setOrderId(orderNumber);
            detail.setNumber(shoppingCart.getNumber());
            detail.setDishFlavor(shoppingCart.getDishFlavor());
            detail.setDishId(shoppingCart.getDishId());
            detail.setSetmealId(shoppingCart.getSetmealId());
            detail.setName(shoppingCart.getName());
            detail.setImage(shoppingCart.getImage());
            detail.setAmount(shoppingCart.getAmount());
            detailList.add(detail);
            amount.addAndGet(shoppingCart.getAmount().multiply(new BigDecimal(shoppingCart.getNumber())).intValue());
        }
        orders.setId(orderNumber);
        orders.setOrderTime(LocalDateTime.now());
        orders.setCheckoutTime(LocalDateTime.now());
        orders.setStatus(1);
        orders.setUserId(userId);
        orders.setNumber(orderNumber+"");
        orders.setUserName(user.getName());
        orders.setConsignee(addressBook.getConsignee());
        orders.setPhone(addressBook.getPhone());
        orders.setAddress(addressBook.getDetail());
        orders.setAmount(new BigDecimal(amount.get()));

        this.save(orders);
        orderDetailService.saveBatch(detailList);
        shoppingCartService.remove(wrapper);
    }

    @Override
    public void again(Orders orders) {
        QueryWrapper<OrderDetail> wrapper=new QueryWrapper<>();
        wrapper.eq("order_id",orders.getId());
        List<OrderDetail> detailList=new ArrayList<>();
        List<ShoppingCart> shoppingCartList=new ArrayList<>();
        for (OrderDetail detail : detailList) {
            ShoppingCart shoppingCart=new ShoppingCart();
            BeanUtils.copyProperties(detail,shoppingCart);
            shoppingCart.setUserId(BaseContext.getCurrentId());
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartList.add(shoppingCart);
        }
        shoppingCartService.saveBatch(shoppingCartList);
    }
}
